package com.weim.study.shiro.controller;

import com.weim.study.shiro.entity.User;
import com.weim.study.shiro.utils.MD5Utils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

@Controller
public class IndexController {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @GetMapping("/index")
    public String index() {
        logger.info("go index");
        return "index";
    }

    @GetMapping("/login")
    public String login() {
        logger.info("go login");
        return "login";
    }

    @GetMapping("/error")
    public String error() {
        logger.info("go error");
        return "error";
    }

    @PostMapping("/login")
    public String login(User user) throws Exception {
        logger.info(user.toString());

        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(
                user.getUsername(),
                user.getPassword());

        //进行验证，这里可以捕获异常，然后返回对应信息
        subject.login(usernamePasswordToken);

        return "user";
    }
}
